package sixkey.utils;

import java.io.Serializable;

/**
 * ClassName:ResResult
 * Package:sixkey.utils
 * Description
 *
 * @Author:@wenxueshi
 * @Create:2023/5/20 - 0:21
 * @Version:v1.0
 */

/**
 * 统一结果响应类
 */
public class ResResult<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;

    //当操作成功，无返回数据时
    public ResResult(){
        this.code = ResCodeEnum.SUCCESS.code;
        this.message = ResCodeEnum.SUCCESS.getMessage();
    }

    //返回码code和数据data
    public ResResult(Integer code, T data){
        this.code = code;
        this.data = data;
    }

    //返回码code和消息message
    public ResResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    //返回码code、消息message、数据集data
    public ResResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static ResResult errorResult(int code, String msg) {
        ResResult result = new ResResult();
        return result.error(code, msg);
    }

    public static ResResult errorResult() {
        ResResult result = new ResResult(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMessage());
        return result;
    }

    public static ResResult okResult() {
        ResResult result = new ResResult();
        return result;
    }

    public static ResResult okResult(int code, String msg) {
        ResResult result = new ResResult();
        return result.ok(code, null, msg);
    }

    public static ResResult okResult(Object data) {
        ResResult result = setAppHttpCodeEnum(ResCodeEnum.SUCCESS, ResCodeEnum.SUCCESS.getMessage());
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static ResResult errorResult(ResCodeEnum enums) {
        return setAppHttpCodeEnum(enums, enums.getMessage());
    }

    public static ResResult errorResult(ResCodeEnum enums, String msg) {
        return setAppHttpCodeEnum(enums, msg);
    }

    public static ResResult setAppHttpCodeEnum(ResCodeEnum enums) {
        return okResult(enums.getCode(), enums.getMessage());
    }

    private static ResResult setAppHttpCodeEnum(ResCodeEnum enums, String msg) {
        return okResult(enums.getCode(), msg);
    }

    public ResResult<?> error(Integer code, String msg) {
        this.code = code;
        this.message = msg;
        return this;
    }

    public ResResult<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public ResResult<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.message = msg;
        return this;
    }

    public ResResult<?> ok(T data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
